package com.github.m6d21.sso.support.validator.phone;

import com.github.m6d21.sso.common.util.PhoneUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *<p>
 *手机号验证器
 *</p>
 *
 * @author liuyang
 * @date 2018-12-06 Thu 16:52:43
 * @since 1.0.0
 */
public class PhoneValidator implements ConstraintValidator<Phone, String> {

    private static final Logger LOG = LoggerFactory.getLogger(PhoneValidator.class);

    private Phone phone;
    @Override
    public void initialize(Phone phone) {
        this.phone = phone;
    }

    @Override
    public boolean isValid(String phone, ConstraintValidatorContext context) {
        //非空校验应该交给@Notblank @NotNull
        if (phone == null || phone.length() == 0) {
            return true;
        }

        return PhoneUtil.checkPhone(phone);
    }
}
